//
//  UIControl+LLAdd.m
//  SLWallet
//
//  Created by mc on 2018/12/12.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "UIControl+LLAdd.h"
#import <objc/runtime.h>
#import "LLMethodSwizzTool.h"

static char *loginKey = "ll_loginKey";

@implementation UIControl (LLAdd)

+ (void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        SEL originalM = @selector(sendAction:to:forEvent:);
        SEL newM = @selector(ll_sendAction:to:forEvent:);
        
        [LLMethodSwizzTool methodSwizzWithClass:self OriginSEL:originalM swizzSEL:newM];
        
    });
}


- (void)ll_sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event
{
    
    // 在这里统一拦截按钮点击的登录事件（tabbar上的按钮除外，需要自己处理）
    if (self.isNeedLogin&& ![TTLoginSingle sharedSingle].isLogin) {
         //TODO: 需要登陆, 弹出登录页面
        TTLoginViewController *loginVC = [[TTLoginViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVC];
        nav.modalPresentationStyle = UIModalPresentationFullScreen;
        [[LLRouteTool getCurrentNavigationController] presentViewController:nav animated:YES completion:nil];
        
        return;
    }
    
    [self ll_sendAction:action to:target forEvent:event];
}


- (BOOL)isNeedLogin
{
    return [objc_getAssociatedObject(self, loginKey) boolValue];
}

- (void)setIsNeedLogin:(BOOL)isNeedLogin
{
    objc_setAssociatedObject(self, loginKey, @(isNeedLogin), OBJC_ASSOCIATION_ASSIGN);
}


@end
